package epam.bookShelf.actions;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import epam.bookShelf.bookStuff.Book;
import epam.bookShelf.database.BookDAO;
import epam.bookShelf.web.ServletListener;

/**
 * Servlet action for searching book via page "searchBook.do"
 * @author Pustovit Michael, pustovitm@gmail.com
 */
public class SearchBook extends Action {
	Logger log = Logger.getLogger(AddBook.class);
	
	@Override
	public void run(HttpServletRequest request, HttpServletResponse response,
			ServletContext context) throws ServletException, IOException 
	{
		String title = request.getParameter("title");
		
		BookDAO dao = new BookDAO(ServletListener.getDAO());
		
		try {
			ArrayList<Book> books = dao.readBookByTitle(title);
			
			request.setAttribute("bookList", books);
			
			context.getRequestDispatcher("/SearchResult.jsp").
			forward(request, response);
		} catch (SQLException e) {
			log.error("Error with book search");
			e.printStackTrace();
		}
	}

}
